package com.hxs.learn.action.core;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import com.hxs.learn.entity.core.SysMenu;
import com.hxs.learn.service.core.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import reactor.core.publisher.Mono;

/**
 * @author huxinsheng
 */
@RestController
@RequestMapping(value = "core/menu")
public class MenuAction extends AbstractViewAction {
    private final SysMenuService sysMenuService;

    @Autowired
    public MenuAction(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    @ApiOperation(
        value    = "删除菜单数据",
        notes    = "菜单管理时删除菜单数据"
    )
    @ApiParam(
        name     = "id",
        value    = "菜单ID",
        required = true
    )
    @PostMapping(ReqConst.REQ_DELETE_DATA_BY_ID)
    @ResponseBody
    public Result deleteData(@Login LoginInfo loginInfo, @PathVariable String id) {
        return menuService.removeById(loginInfo, id);
    }

    @GetMapping(ReqConst.REQ_INDEX)
    public ModelAndView index(@Login LoginInfo sysUser) {
        return createModelAndView(sysUser);
    }

    @ApiOperation(
        value    = "保存菜单数据",
        notes    = "菜单管理时保存菜单数据"
    )
    @ApiParam(
        name     = "菜单对象",
        value    = "传入json格式",
        required = true
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyData(@Login LoginInfo loginInfo, SysMenu menu) {
        return menuService.save(loginInfo, menu);
    }

    @ApiOperation(
        value = "编辑菜单页面",
        notes = "菜单管理时显示编辑菜单页面"
    )
    @ApiParam(
        name  = "id",
        value = "菜单ID"
    )
    @GetMapping(ReqConst.REQ_MODIFY_DATA_DIALOG)
    public ModelAndView modifyDataDialog(Model model, String id) {
        SysMenu menu = menuService.findById(id);

        // 查询一级菜单
        model.addAttribute(PAGE_DATA, menu);

        return createModelAndView(ViewNames.PAGE_MENU_MODIFY);
    }

    @ApiOperation(
        value = "编辑子菜单页面",
        notes = "菜单管理时显示编辑子菜单页面"
    )
    @ApiParam(
        name  = "id",
        value = "菜单ID"
    )
    @GetMapping(ReqConst.REQ_MODIFY_SUB_DATA_DIALOG)
    public ModelAndView modifySubDataDialog(Model model, String id) {
        SysMenu menuPojo   = menuService.findById(id);
        SysMenu parentMenu = new SysMenu();

        parentMenu.setParentName(menuPojo.getName());
        parentMenu.setPid(menuPojo.getId());

        // 查询一级菜单
        model.addAttribute(PAGE_DATA, parentMenu);

        return createModelAndView(ViewNames.PAGE_MENU_MODIFY);
    }

    @ApiOperation(
        value = "获取菜单页面数据",
        notes = "菜单管理时获取菜单页面数据"
    )
    @ResponseBody
    @GetMapping(ReqConst.REQ_QUERY_PAGE_DATA)
    public Object queryPageData(SysMenu sysMenu) {
        return Mono.create(resultMonoSink -> resultMonoSink.success(sysMenuService.findSysMenuTreeData(sysMenu)));
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
